package system.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import system.common.Arguments;
import system.common.Constants;
import system.dao.DB51_NetKeyDao;
import system.dao.DB52_NetKeyDao;
import system.model.DB5_NetKeyDto;
import system.util.FileUtil;

@Service
public class DB5_NetKeyService {
	@Autowired
	private DB51_NetKeyDao db51_NetKeyDao;
	
	@Autowired
	private DB52_NetKeyDao db52_NetKeyDao;

	public void net_key_01(Arguments arguments) throws IOException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getParameter2() + "_" + arguments.getParameter3();
		String historyFileName = Constants.NET_KEY_01_History + Constants.NET_KEY_01 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.NET_KEY_01_Current + Constants.NET_KEY_01;
		
		// check for directory
		FileUtil.directoryCheck(Constants.NET_KEY_01_History);
		FileUtil.directoryCheck(Constants.NET_KEY_01_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.NET_KEY_01, Constants.NET_KEY_01_Current);

		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		// rs start
		long start = System.currentTimeMillis();
		
		List<DB5_NetKeyDto> net_key_01_list = db51_NetKeyDao.net_key_01_list(arguments);
		Constants.totalRecord = net_key_01_list.size();

		long end = System.currentTimeMillis();
		Constants.rsTime = (end - start) / 1000f;
		// rs end
		// file-make start
		start = System.currentTimeMillis();
		
		file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
		bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

		ExportService exportService = new ExportService();
		exportService.OutputList(net_key_01_list, bufferedWriter, historyFileName, "_0001", 0, Constants.NET_KEY_01);
		
		end = System.currentTimeMillis();
		Constants.fileMakeTime = (end - start) / 1000f;
		// file-make end
		// file-copy start
		start = System.currentTimeMillis();
		
		FileUtil.copyBytes(Constants.NET_KEY_01_History, Constants.NET_KEY_01 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

		end = System.currentTimeMillis();
		Constants.fileCopyTime = (end - start) / 1000f;
		// file-copy end
	}
	
	public void net_key_02(Arguments arguments) throws IOException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getParameter2() + "_" + arguments.getParameter3();
		String historyFileName = Constants.NET_KEY_02_History + Constants.NET_KEY_02 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.NET_KEY_02_Current + Constants.NET_KEY_02;
		
		// check for directory
		FileUtil.directoryCheck(Constants.NET_KEY_02_History);
		FileUtil.directoryCheck(Constants.NET_KEY_02_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.NET_KEY_02, Constants.NET_KEY_02_Current);

		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		// rs start
		long start = System.currentTimeMillis();
		
		List<DB5_NetKeyDto> net_key_02_list = db52_NetKeyDao.net_key_02_list(arguments);

		long end = System.currentTimeMillis();
		Constants.rsTime = (end - start) / 1000f;
		// rs end
		// file-make start
		start = System.currentTimeMillis();
		
		file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
		bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

		ExportService exportService = new ExportService();
		exportService.OutputList(net_key_02_list, bufferedWriter, historyFileName, "_0001", 0, Constants.NET_KEY_02);
		
		end = System.currentTimeMillis();
		Constants.fileMakeTime = (end - start) / 1000f;
		// file-make end
		// file-copy start
		start = System.currentTimeMillis();
		
		FileUtil.copyBytes(Constants.NET_KEY_02_History, Constants.NET_KEY_02 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

		end = System.currentTimeMillis();
		Constants.fileCopyTime = (end - start) / 1000f;
		// file-copy end
	}
}
